/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        ListNode* head,*p1=pHead1,*p2=pHead2;

		int len1=0,len2=0;
		while(p1)
		{
			len1++;
			p1=p1->next;
		}
		while(p2)
		{
			len2++;
			p2=p2->next;
		}

		int dif(abs(len1-len2));
		if(len1>len2)
		{
			while(pHead1 && dif)
			{
				pHead1=pHead1->next;
				dif--;
			}
		}
		else{
			while(pHead2 && dif)
			{
				pHead2=pHead2->next;
				dif--;
			}
		}
		while(pHead1 && pHead2 && pHead1->val!=pHead2->val)
		{
			pHead1=pHead1->next;
			pHead2=pHead2->next;
		}
		if(pHead1 && pHead2)
			return pHead1;
		return nullptr;
    }
};
